package net;

import java.io.*;

public class Test_5 {
    public static void main(String[] args) {
        Dog dog = new Dog("小黄",5);
        Dog dog2=new Dog("小欧",2);

        String path="D:\\javacode\\javacode\\J2025_1_3\\dog.dat";
        ObjectOutputStream oos=null;
        ObjectInputStream ois=null;

        try {
            oos=new ObjectOutputStream(new FileOutputStream(path));
            ois=new ObjectInputStream(new FileInputStream(path));

            oos.writeObject(dog);
            oos.writeObject(dog2);

            Object o1 = ois.readObject();
            Object o2 = ois.readObject();

            Dog d1=(Dog) o1;
            Dog d2=(Dog) o2;

            System.out.println(d1);
            System.out.println(d2);

        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                oos.close();
                ois.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }


    }
}

class Dog implements Serializable{
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}